package dshumsky.web.location;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

@Controller(LocationComponentAction.BEAN_NAME)
public class LocationComponentAction implements InitializingBean {
	public static final String BEAN_NAME = "LocationComponentAction";

	public LocationComponentAction() {
		System.out.println("1");
	}

	@RequestMapping(method = RequestMethod.GET, value = "/searchNode.spring")
	public void searchNode(
			@RequestParam(value = "parent", required = false) String parent,
			@RequestParam(value = "filter", required = true) final String filter,
			final HttpServletResponse resp, final HttpSession session)
			throws IOException {
		ObjectMapper mapper = new ObjectMapper();
		ArrayList<LocationNode> list = new ArrayList<LocationNode>(100);
		for (int i = 0; i < 100; i++) {
			LocationNode locationNode = new LocationNode();
			locationNode.id = i;
			locationNode.name = "name" + i;
			list.add(locationNode);
		}
		mapper.writeValue(resp.getOutputStream(), list);
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		System.out.println("2");
	}
}
